package org.cosmact.designer.vxml.gefeditor.figures;

import static org.eclipse.draw2d.PositionConstants.CENTER;

import org.eclipse.draw2d.AbstractBackground;
import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.FreeformLayout;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.ImageFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.geometry.Insets;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;

/**
 * A rectangle with an image as it's icon
 * 
 *@author hsieh 234aini@gmail.com
 * 
 */
public class DialogContainerFigure extends Figure {

	private static final int IMAGE_OFFSET = 16;
	private static final int LABEL_OFFSET = 10;
	private static final int ICON_SIZE = 48;

	public static final int DIALOG_SIZE_WIDTH_DEFAULT = ICON_SIZE + 2
			* IMAGE_OFFSET;

	public static final int DIALOG_SIZE_HEIGHT_DEFAULT = ICON_SIZE + 2
			* IMAGE_OFFSET;

	private static final Font LABEL_FONT = new Font(Display.getCurrent(),
			"Arial", 8, SWT.NORMAL);

	private ImageFigure iconFigure;
	private Label nameLabel;
	private AbstractBackground backGroundBorder;

	public DialogContainerFigure(Image icon, String label) {
		setLayoutManager(new FreeformLayout());
		iconFigure = new ImageFigure(icon);
		add(iconFigure);

		nameLabel = new Label(label);
		nameLabel.setFont(LABEL_FONT);
		nameLabel.setLabelAlignment(CENTER);
		add(nameLabel);

		backGroundBorder = new AbstractBackground() {
			@Override
			public void paintBackground(IFigure figure, Graphics graphics,
					Insets insets) {
				graphics.setBackgroundColor(ColorConstants.tooltipBackground);
				graphics.fillRectangle(getBounds());
			}
		};

	}

	@Override
	protected void paintFigure(Graphics graphics) {
		if (true) {
			// TODO User set decide display the border or not.
			setBorder(backGroundBorder);
		}
		Rectangle bound = getBounds();
		iconFigure.setLocation(new Point(bound.x + IMAGE_OFFSET, bound.y
				+ IMAGE_OFFSET));
		iconFigure.setSize(ICON_SIZE, ICON_SIZE);

		Point point = iconFigure.getLocation().getCopy();
		nameLabel.setSize(getBounds().width - LABEL_OFFSET, IMAGE_OFFSET);
		nameLabel.setLocation(new Point(getBounds().x + LABEL_OFFSET / 2,
				point.y + iconFigure.getBounds().height));
		super.paintFigure(graphics);
	}
}
